﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using PMS.BLL;
using PMS.Entity;
using PMS;
using PMS.Resources;
using PMS.Win;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Base;
using PMS.Utils;
using System.Collections;

namespace PMS.ChucNang
{
    public partial class BienBanKiemKeKhoLookup : FormPopupBase
    {

        private readonly BienBanKiemKeKhoBO service = new BienBanKiemKeKhoBO();
        public BienBanKiemKeKho BienBanKiemKeKhoSelected;
        private List<ComboItem> m_Ketluan;
        public BienBanKiemKeKhoLookup()
        {
            InitializeComponent();
            InitStyle();
            InitData();
        }
      
        void InitStyle()
        {
            grvBienBanKiemKeKhoList.OptionsBehavior.Editable = false;
            grvBienBanKiemKeKhoList.OptionsView.ShowGroupPanel = false;
            grvBienBanKiemKeKhoList.OptionsView.ShowIndicator = true;

            grvBienBanKiemKeKhoList.OptionsView.ShowAutoFilterRow = true;
            grvBienBanKiemKeKhoList.OptionsView.ShowFilterPanelMode = ShowFilterPanelMode.Never;
            grvBienBanKiemKeKhoList.OptionsView.ShowGroupedColumns = false;
            grvBienBanKiemKeKhoList.OptionsMenu.EnableColumnMenu = false;
            grvBienBanKiemKeKhoList.OptionsCustomization.AllowColumnMoving = false;
            grvBienBanKiemKeKhoList.OptionsCustomization.AllowGroup = false;
            grvBienBanKiemKeKhoList.OptionsFilter.AllowFilterEditor = false;

            this.grvBienBanKiemKeKhoList.OptionsSelection.MultiSelect = false;
            this.grvBienBanKiemKeKhoList.IndicatorWidth = Constants.INDICATOR_WIDTH;
            //gridbinding
            this.colMaBBKK.FieldName = BienBanKiemKeKhoColumns.MaBBKKK.ToString();
            this.colThang.FieldName = BienBanKiemKeKhoColumns.Thang.ToString();
            this.colNam.FieldName = BienBanKiemKeKhoColumns.Nam.ToString();
            this.colNguoiThucHien.FieldName = BienBanKiemKeKhoColumns.NguoiThucHien.ToString();
            this.colNgayThucHien.FieldName = BienBanKiemKeKhoColumns.NgayThucHien.ToString();
            this.colThuKho.FieldName = BienBanKiemKeKhoColumns.ThuKho.ToString();
            this.colKeToan.FieldName = BienBanKiemKeKhoColumns.KeToan.ToString();
            this.colGiamDoc.FieldName = BienBanKiemKeKhoColumns.GiamDoc.ToString();
            this.colKetLuan.UnboundType = DevExpress.Data.UnboundColumnType.String;
        }

        void InitData()
        {
            LoadDatasourceCbbKetluan();
            try
            {
                
                    this.grdBienBanKiemKeKhoList.DataSource = service.GetAllBienBanKiemKeKho();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }

        }
        private void LoadDatasourceCbbKetluan()
        {
            ArrayList tempItemList = BindDataUtil.BindDataCombobox("BBKKKKetluan", typeof(BBKKKKetluan));
            m_Ketluan = new List<ComboItem>();
            foreach (ComboItem item in tempItemList)
            {
                m_Ketluan.Add(item);
            }
        }

        private void CloseDialog(DialogResult result)
        {
            try
            {
                if (result == DialogResult.OK)
                {
                    this.BienBanKiemKeKhoSelected = (BienBanKiemKeKho)grvBienBanKiemKeKhoList.GetFocusedRow();
                }
                else
                {
                    this.BienBanKiemKeKhoSelected = null;
                }

                this.DialogResult = result;
            }
            catch (Exception ex)
            {
                ShowErrorMessage(ex);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.OK);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            CloseDialog(DialogResult.Cancel);
        }

        private void grvBienBanKiemKeKhoList_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if ((e.Info.IsRowIndicator) && (e.RowHandle >= 0))
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        private void grvBienBanKiemKeKhoList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = sender as GridView;
            GridHitInfo hi = view.CalcHitInfo(grdBienBanKiemKeKhoList.PointToClient(MousePosition));
            if (hi.InRow || hi.InRowCell)
            {
                if (BienBanKiemKeKhoSelected != null)
                {
                    CloseDialog(DialogResult.OK);
                }
            }
        }

        private void grvBienBanKiemKeKhoList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                btnOk_Click(sender, null);
            }
            else if (e.KeyCode == Keys.Escape)
            {
                btnCancel_Click(sender, null);
            }
        }

        private void grvBienBanKiemKeKhoList_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            BienBanKiemKeKhoSelected = this.grvBienBanKiemKeKhoList.GetFocusedRow() as BienBanKiemKeKho;
        }

        private void grvBienBanKiemKeKhoList_ShowingEditor(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }

        private void grvBienBanKiemKeKhoList_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            BienBanKiemKeKho tempRow = (BienBanKiemKeKho)this.grvBienBanKiemKeKhoList.GetRow(e.RowHandle);
            if ((e.Column == colKetLuan) && e.IsGetData && e.RowHandle >= 0)
            {
                foreach (ComboItem item in m_Ketluan)
                {
                    if (tempRow.KetLuan == (byte)item.ItemValue)
                    {
                        e.Value = item.DisplayValue;
                        break;
                    }
                }

            }
        }
    }
}
